package com.huluobo.a2201mvvm_imdemo.demo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.media.Image;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by LC on 2024/3/27.
 */
public class MyView extends View {
    private int progress = 0;
    private String timeString;

    //一个参数的构造方法一般用在Java中
    //   例子: ImageView imageView = new ImageView(getContext());
    public MyView(Context context) {
        super(context);
    }

    //2个参数的构造方法,一般用在xml中,AttributeSet 属性集合,在xml中写的所有属性都会存放在这里
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    private Paint paint;//画笔
    private Paint paintText;

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);//画笔设置颜色
        paint.setStyle(Paint.Style.STROKE);//设置绘制线
        paint.setStrokeWidth(5);//设置线的宽度
        paint.setAntiAlias(true);
//        paint.setTextSize(100);
        paintText = new Paint();
        paintText.setColor(Color.BLACK);
        paintText.setTextSize(200);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //给一个圆限定一个范围,距离LTRB 都是距离顶部或者左侧的距离,所以这个矩形是个500*500的正方形
        //其中的圆就是一个直径为500的圆 半径250
        RectF rectF = new RectF(100, 100, 600, 600);

        //参数: 范围(上面哪个矩形) 起始角度(注意绘制时 最右侧的点为0度) 终点的角度,是否使用圆心连接,画笔
        canvas.drawArc(rectF, 270, progress, false, paint);
        canvas.drawText(timeString, 300, 410, paintText);
    }

    public void start(int time) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);//给圆角度的变化的值
        valueAnimator.addUpdateListener(animation -> {
            progress = (int) animation.getAnimatedValue();//获取更新的值
            invalidate();//view刷新自身的方法,更新画圆的进度
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setDuration(time * 1000L);
        valueAnimator.start();

        ValueAnimator valueAnimatorText = ValueAnimator.ofInt(time, 0);//倒数,参数给了几秒就倒数几秒
        valueAnimatorText.addUpdateListener(animation -> {
            timeString = String.valueOf(animation.getAnimatedValue());
            invalidate();
        });
        valueAnimatorText.setInterpolator(new LinearInterpolator());
        valueAnimatorText.setDuration(time * 1000L);
        valueAnimatorText.start();

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //当动画结束时,通过接口通知外部
                //4.在想要触发的位置调用接口中的方法
                onAniEndListener.onAnimEnd();
            }
        });
    }

    //接口回调
    //1.创建一个接口
    public interface OnAniEndListener {
        void onAnimEnd();
    }

    //2.在当前类中声明接口
    private OnAniEndListener onAniEndListener;

    //3.给接口生成一个set方法

    public void setOnAniEndListener(OnAniEndListener onAniEndListener) {
        this.onAniEndListener = onAniEndListener;
    }
}
